﻿using System;
using System.ComponentModel.DataAnnotations;
using User.EF.Contracts;
using User.EF.Model;
using User.Global.Contracts.Message;
using User.Global.Resources;
using User.Web.Contracts.Model.Admin;

namespace User.Web.Models.Admin
{
    /// <summary>
    /// Model nur zum Anzeigen der MemberDetails
    /// </summary>
    public class MemberDetailsModel : ModelBase, IMemberDetailsModel
    {
        #region Member
        /// <summary>
        /// Die Benutzeranrede
        /// </summary>
        [Display(ResourceType = typeof(InputFieldNames), Name = "Title")]
        public string Title { get; set; }

        /// <summary>
        /// der Vorname des Members
        /// </summary>
        [Display(ResourceType = typeof(InputFieldNames), Name = "Firstname")]
        public string Firstname { get; set; }
        
        /// <summary>
        /// Der Nachname des Members
        /// </summary>
        [Display(ResourceType = typeof(InputFieldNames), Name = "Lastname")]
        public string Lastname { get; set; }
        
        /// <summary>
        /// Der Loginname (E-Mail) des Members
        /// </summary>
        [Display(ResourceType = typeof(InputFieldNames), Name = "Username")]
        public string Username { get; set; }
        
        /// <summary>
        /// Die ID des Members im System
        /// </summary>
        public long Id { get; set; }
        
        /// <summary>
        /// Das letzte Logindatum/Uhrzeit des Members
        /// </summary>
        [Display(ResourceType = typeof(InputFieldNames), Name = "LastLoginDate")]
        public DateTime LastLoginDate { get; set; }
        
        /// <summary>
        /// Der Name des Providers mit dem sich der Member einloggt
        /// </summary>
        [Display(ResourceType = typeof(InputFieldNames), Name = "Providername")]
        public string ProviderName { get; set; }
        
        /// <summary>
        /// Gibt an ob der Account noch aktiviert ist oder deaktiviert wurde.
        /// </summary>
        [Display(ResourceType = typeof(InputFieldNames), Name = "IsActiveAccount")]
        public bool IsActiveAccount { get; set; }

        /// <summary>
        /// Die zugewiesenen Benutzerrechte des Accounts
        /// </summary>
        [Display(ResourceType = typeof(InputFieldNames), Name = "AssignedRights")]
        public string AssignedRights { get; set; }

        /// <summary>
        /// Die Telefonnumer des Members
        /// </summary>
        [Display(ResourceType = typeof(InputFieldNames), Name = "Phone")]
        public string Mobilenumber { get; set; }

        /// <summary>
        /// Anzahl der zuletzt versuchten Loginversuche.
        /// </summary>
        [Display(ResourceType = typeof(InputFieldNames), Name = "LoginTries")]
        public int LoginTries { get; set; }
        #endregion

        #region Initialize
        public MemberDetailsModel()
        {
            Id = 0;
            Firstname = string.Empty;
            Lastname = string.Empty;
            Username = string.Empty;
            LoginTries = 0;
            LastLoginDate = DateTime.Now.AddDays(10);
            ProviderName = string.Empty;
            IsActiveAccount = false;
            AssignedRights = string.Empty;
            Mobilenumber = string.Empty;
        }
        #endregion

        #region Public Functions
        /// <summary>
        /// Laden der passenden Modeldaten für den View.
        /// </summary>
        /// <returns>TRUE->Laden der Daten erfolgreich | FALSE->Fehler beim Laden der Daten</returns>
        public bool Load(IWebQuerySummary webQuery, long userId)
        {
            //Unseren User laden
            Member member = webQuery.MembershipQueries.GetMemberById(userId);
            if (member != null)
            {
                Id = member.Id;
                Username = member.Username;
                Title = member.MemberExtension != null ? member.MemberExtension.Title.ToString() : DefaultMessages.KeinAngabe;
                Firstname = member.MemberExtension != null ? member.MemberExtension.Firstname : DefaultMessages.KeinAngabe;
                Lastname = member.MemberExtension != null ? member.MemberExtension.Lastname : DefaultMessages.KeinAngabe;
                Mobilenumber = member.MemberExtension != null ? member.MemberExtension.Mobilenumber : DefaultMessages.KeinAngabe;
                IsActiveAccount = member.IsActive;
                ProviderName = member.MemberOAuth != null ? member.MemberOAuth.Provider : DefaultMessages.KeinAngabe;
                LastLoginDate = member.LastLoginTry;
                LoginTries = member.LoginTries;

                //Die zugewiesenen Rechte ermitteln.
                foreach (MemberRights right in member.MemberRights)
                {
                    if (AssignedRights.Length > 0)
                    {
                        AssignedRights += "; ";
                    }

                    AssignedRights += RightNames.ResourceManager.GetString(right.MemberRight.ToString());
                }

                if (AssignedRights.Length == 0)
                {
                    AssignedRights = DefaultMessages.KeinAngabe;
                }

                return true;
            }

            AddMessage.Add(ErrorMessages.UserDontExists, MessageTypes.error);
            return false;
        }
        #endregion
    }
}
